// 云函数入口文件
const cloud = require('wx-server-sdk')
const axios = require('axios')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境

const db = cloud.database();
const Animalcollection = db.collection('c_animal')
const _ = db.command


// 云函数入口函数
exports.main = async (event, context) => {
  // const wxContext = cloud.getWXContext()

  // const { GET } = event

  // try {
  //   const res = await axios.get(`http://120.78.75.230:8080/animal/${GET}`)
  //   console.log(res);

  //   return {
  //     code: res.data.code,
  //     msg: res.data.msg,
  //     data: res.data.data
  //   }
  // } catch (error) {
  //   return {
  //     code: -1001,
  //     msg: '请求数据失败',
  //     data: err,
  //   };
  // }

  const repeatInfo = []
  const perferInfo = []
  Animalcollection.get().then(res => {
    // console.log(res);
    const animal = res.data
    for (let item of animal) {
      console.log(item);
      for (let perferItem of perferInfo) {
        if (perferItem.animalId === item.animalId) {
          repeatInfo.push(perferItem._id)
          return
        }
      }
      perferInfo.push(item)
    }
  })
  console.log(repeatInfo);
  console.log(perferInfo);

  return {
    code: 200,
    data: Animalcollection.get()
  }
}
